package kotlinquanweizhinan.chapt05匿名函数与函数类型.类型推断

class SimVillage {
}

fun main() {

        val greetingFunction0:()->String = {
                val currentYear = 2018
                "Welcome to SimVillage,Mayour!(copyright $currentYear)"
        }
        //使用类型推断，去掉变量的类型声明
        val greetingFunction1 = {
                val currentYear = 2018
                "Welcome to SimVillage,Mayour!(copyright $currentYear)"
        }
        /**
         * 和前面自动推断变量类型一样，Kotlin的类型推断规则同样也适用函数类型:定义一个变量时，如果
         * 已把匿名函数作为变量赋值给它，就不需要显式指明变量类型了。
         * 类型推断也支持事带参数的匿名函数，但为了帮助编译器更准确地推断变量类型，匿名函数的参数名和参数
         * 类型必须有。
         */
        //使用类型推断
        //val greetingFunction:(String,Int) -> String = {playerName:String,numBuildings:Int ->
        val greetingFunction = {playerName:String,numBuildings:Int ->
                val currentYear = 2018
                println("Adding $numBuildings house")
                "Welcome to SimVillage,$playerName!(copyright $currentYear)"
        }
}